<?php
/** Function: printFullTaskList
  * ---------------------------
  * Writes the default task list for the home and project
  * screens. Uses the <li> tag, assuming the data will
  * appear within an existing unordered list.
  *
  * @param user_id: The user whose tasks are to be printed
  * @param database: The path to the SQLite database
  * @param completed: Determines whether to print tasks
  *		are completed (1), incomplete (0), or both (2).
  * @param project: If not 0, only tasks from the project
  * 	with this ID will be printed
  * @param checked: boolean indicating whether checkboxes
  * 	next to task names should be checked.
  *	@param sort: Whether to sort tasks with oldest due date
  * 	at the top (ASC) or the most recent due date at the top
  * 	(DESC)
  */
function printFullTaskList($user_id, $database, $completed=0, $project=0, $checked=false, $sort="ASC")	{
	if($db = new SQLiteDatabase($database)){
		$command = "SELECT * FROM tasks	WHERE deleted=0 ";
		if($project == 0)	{
		 $command .= "AND (user_id=%d OR assigned_to=%d) ";
		 $command=sprintf($command,$user_id,$user_id);
		}
		if($completed == 0)	{
			$command .= "AND completed=0 ";
		}	else if($completed == 1)	{
			$command .= "AND completed=1 ";
		}
		if($project != 0)	{
			$command .= "AND project_id=$project ";
		}
		$command .= "ORDER BY due_time_from " . $sort;
		$result = $db->query($command);
		while($result->valid()){
			$cur = $result->current();
				echo("<li class=\"arrow task touch");
				if($cur['help_required'] == 1) echo(' help');
				if($project != 0)	{
					if($cur['assigned_to'] == 0)	{
						echo(" unassigned");
					}	else if($cur['assigned_to'] == $user_id)	{
						echo(" mine");
					}	else	{
						echo(" delegated");
					}
				}
				echo("\"><input type=\"checkbox\" class=\"taskbox\" ");
				if(!$project) echo("id=\"taskbox");
				else	echo("id=\"ptskbox");
				echo($cur['task_id'] . "\" "
					. ($checked ? "checked=\"checked\" " : '')
					. " /><span class=\"taskname\">".stripslashes($cur['task_name'])."</span><span class=\"due"
					. ($cur['due_time_from'] < date('U') ? " overdue" : "")
					. "\">");
		if(date('U',$cur['due_time_from']) < 2011996800)	{
			if(date('Ymd',$cur['due_time_from']) == date('Ymd'))	{
				echo("Today");
			}	else if(date('Ymd',$cur['due_time_from']) == date('Ymd', (date('U') + 86400)))	{
				echo("Tomorrow");
			}	else	{
				echo($cur['due_time_from'] < (date('U') + 604800) && date('Ymd',$cur['due_time_from']) > date('Ymd', date('U'))  ? date('l', $cur['due_time_from']) : date('F j', $cur['due_time_from']));
			}
		}
		echo("</span></li>\n");
		echo("<div class='spacer' />\n");
			$result->next();
		}
	} else {
		die($err);
	}
}

/** Function: printDateSelector
  * ---------------------------
  * Prints a form to select a date and time.
  * @param id: unique identifier for the selects. This ID
  * 	will have _date, _month, etc appended to it.
  * @param class: a class to add to each select.
  */
function printDateSelector($id, $class)	{
	echo("<select id=\"" . $id . "_month\" class=\"$class date\">");
	for($i = 1; $i <= 12; $i++)
		echo("<option value=\"$i\">" . date('M', mktime(12, 0, 0, $i, 2, 2009)) . "</option>\n");
	echo("</select><select id=\"" . $id . "_date\" class=\"$class date\">");
	for($i = 1; $i <= 31; $i++)
		echo("<option value=\"$i\">$i</option>");
	echo("</select><select id=\"" . $id . "_year\" class=\"$class date\">");
	for($i = date('Y'); $i <= (date('Y') + 3); $i++)
		echo("<option value=\"$i\">$i</option>");
	echo("</select><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at <select id=\"" . $id . "_hour\" class=\"$class date\">");
	for($i = 0; $i <= 23; $i++)
		echo("<option value=\"$i\">$i</option>");
	echo("</select>:<select id=\"" . $id . "_minute\" class=\"$class date\">");
	for($i = 0; $i < 60; $i+=5)
		echo("<option value=\"$i\">" . ($i < 10 ? '0' . $i: $i) . "</option>");
	echo("</select>");
}

/** Function: printProjectList
  * --------------------------
  * Prints an unordered list of projects, including both projects
  * created by the given user and projects of which that user is
  * a member.
  * @param user_id: the user id of the user whose projects are to
  * 	be printed.
  * @param database: the address of the SQLite database to read from.
  */
function printProjectList($user_id, $database)	{
	if($db = new SQLiteDatabase($database)){
		$command = "SELECT * FROM projects,project_membership
		WHERE projects.deleted=0 AND project_membership.deleted=0
			AND projects.project_id=project_membership.project_id 
			AND project_membership.user_id=%d
			ORDER BY project_name ASC";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li class=\"arrow project\"><a href=\"#\" class=\"projectLink slide\" data-id=\"" . $cur['projects.project_id'] . "\">" . stripslashes($cur['projects.project_name']) . "</a></li>\n";
			$result->next();
		}
	} else {
		die($err);
	}
}

/** Function: printProjectSelect
  * ----------------------------
  * Prints the <option> elements of an HTML form select
  * to be used in assigning tasks to projects.
  * @param user_id: the user id of the user whose projects
  * 	should be printed.
  * @param database: the address of the SQLite database to
  * 	read from.
  */
function printProjectSelect($user_id, $database)	{
	if($db = new SQLiteDatabase($database)){
		$command = "SELECT * FROM projects,project_membership
		WHERE projects.deleted=0 AND project_membership.deleted=0
			AND projects.project_id=project_membership.project_id 
			AND project_membership.user_id=%d
			ORDER BY project_name ASC";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		echo("<option value=\"0\">None</option>");
		while($result->valid()){
			$cur = $result->current();
			print "<option value=\"" . $cur['projects.project_id'] . "\">" . stripslashes($cur['projects.project_name']) . "</option>\n";
			$result->next();
		}
	} else {
		die($err);
	}
}
/*
function displayContacts($user_id, $database){
	if($db = new SQLiteDatabase($database)){
		$command = "SELECT * FROM users
		WHERE deleted=0";
		$replaced=sprintf($command,$user_id);
		$result = $db->query($replaced);
		while($result->valid()){
			$cur = $result->current();
			print "<li><input type=\"checkbox\" id=\"pshare" . $cur['user_id'] . "\" title=\"".$cur['username']."\" /></li>\n";
			$result->next();
		}
	} else {
		die($err);
	}
}*/
?>
